package org.neframework.webtest.action.user.admin;

import java.util.List;
import java.util.Map;

import org.neframework.webtest.base.BaseController;
import org.neframework.webtest.model.t_fk_role;
import org.neframework.webtest.model.t_fk_user;
import org.neframework.webtest.service.UserService;
import org.neframework.webtest.util.web.session.SessionTools;

import com.tmsps.neframework.core.utils.JsonTools;
import com.tmsps.neframework.core.utils.MD5;
import com.tmsps.neframework.mvc.annotation.Controller;
import com.tmsps.neframework.mvc.annotation.RequestMapping;
import com.tmsps.neframework.mvc.annotation.ResponseBody;
import com.tmsps.neframework.mvc.plugin.Tip;

@Controller
@RequestMapping("/cp/user/admin")
public class AdminController extends BaseController {

	private static final UserService userService = new UserService();

	@RequestMapping("/list")
	public String list() {
		return "/jsp/user/admin/list.jsp";
	}

	@RequestMapping("/list_data")
	@ResponseBody
	public void list_data(String srh_uname) {

		List<Map<String, Object>> list = userService.selectAdminList(srh_uname, sort_params, page);

		result.put("list", list);
	}

	@RequestMapping("/add")
	@ResponseBody
	public void add(t_fk_user admin) {
		admin.setType("管理员");
		admin.setPwd(MD5.md5(admin.getPwd()));
		oc.saveObj(admin);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/edit_form")
	@ResponseBody
	public String edit_form(String kid) {
		t_fk_user admin = (t_fk_user) oc.findObjById(kid, t_fk_user.class);
		t_fk_role role = (t_fk_role) oc.findObjById(admin.getRole_id(), t_fk_role.class);

		Map<String, Object> map1 = JsonTools.objToMap("admin", admin);
		Map<String, Object> map2 = JsonTools.objToMap("role", role);

		return JsonTools.toJson(map1, map2);
	}

	/**
	 * 密码重置
	 * 
	 * @param admin
	 */
	@RequestMapping("/resetpwd")
	@ResponseBody
	public void resetpwd(String kid) {

		t_fk_user adminDb = (t_fk_user) oc.findObjById(kid, t_fk_user.class);
		adminDb.setPwd(MD5.md5("123456"));
		oc.updateObj(adminDb);

		this.setTipMsg(true, "密码重置成功!", Tip.Type.success);
	}

	/**
	 * 限制登陆
	 * 
	 * @param admin
	 */
	@RequestMapping("/limit")
	@ResponseBody
	public void limit(String kid) {

		t_fk_user adminDb = (t_fk_user) oc.findObjById(kid, t_fk_user.class);

		if (adminDb.getStatus_sys().equals("解除登陆") || adminDb.getStatus_sys() == null) {
			adminDb.setStatus_sys("限制登陆");
			oc.updateObj(adminDb);
			this.setTipMsg(true, "限制登陆成功!", Tip.Type.success);

		} else if (adminDb.getStatus_sys().equals("限制登陆")) {
			adminDb.setStatus_sys("解除登陆");
			oc.updateObj(adminDb);
			this.setTipMsg(true, "解除登陆限制成功!", Tip.Type.success);
		}
	}

	@RequestMapping("/edit")
	@ResponseBody
	public void edit(t_fk_user admin) {
		t_fk_user adminDb = (t_fk_user) oc.findObjById(admin.getKid(), t_fk_user.class);
		adminDb.setUname(admin.getUname());
		adminDb.setName(admin.getName());
		adminDb.setSex(admin.getSex());
		adminDb.setRole_id(admin.getRole_id());
		oc.updateObj(adminDb);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/del")
	@ResponseBody
	public void del(String kid) {
		oc.delObjById(kid, t_fk_user.class);

		this.setTipMsg(true, "删除成功!", Tip.Type.success);
	}

	@RequestMapping("/pwd")
	public String pwd_form() {
		return "/jsp/user/admin/pwd.jsp";
	}

	@RequestMapping("/pwd_form")
	@ResponseBody
	public void pwd(String currentpwd, String newpwdone) {

		t_fk_user currentUser = (t_fk_user) SessionTools.getCurrentLoginAdmin(req);

		if (MD5.md5(currentpwd).equals(currentUser.getPwd())) {
			t_fk_user userDb = (t_fk_user) oc.findObjById(currentUser.getKid(), t_fk_user.class);
			userDb.setPwd(MD5.md5(newpwdone));
			oc.updateObj(userDb);
			super.setTipMsg(true, "修改密码成功", Tip.Type.success);
		} else {
			super.setTipMsg(true, "当前密码错误", Tip.Type.error);
		}

	}

}
